<?php

App::uses('AppController', 'Controller');

class GalleryController extends AppController
{
    public $name = 'Gallery';
    public $uses = array('Photo', 'Image');
//    public $components = array('RequestHandler');
    public $helpers = array('Tinymce');
     
    
    function beforeFilter() {
       
    }
    
    // Páginas do CMS AQUI
    function admin_index()
    {
        $this->layout = 'admin';
        
        $Photos = $this->Photo->find('all');
        $this->set('Photos', $Photos);
        
        
//        die(debug($Photos));
    }
    
    function admin_management()
    {
        $this->layout = 'admin';
        
        $Photos = $this->Photo->find('all');
        $this->set('Photos', $Photos);
    }

    function admin_add()
    {
        $this->layout = 'admin';
        
        if($this->request->is('post'))
        {
           if(empty($this->data['Image']['filename']['type']))
           {
               $this->Session->setFlash('<div class="alert alert-error">Arquivo não selecionado</div>');
           }
           else
           {
                if(!isset($this->request->data['Photo']['type'])){$this->request->data['Photo']['type'] = 0;}
                
               $this->Image->save($this->request->data);
               $this->Photo->save(array(
                   'image_id' => $this->Image->getInsertID(),
                   'title' => $this->data['Photo']['title'],
                   'content' => $this->data['Photo']['content'],
                   'type' => $this->data['Photo']['type'],
               ));
               $this->Session->setFlash('<div class="alert alert-success">Enviado com sucesso!</div>');
               $this->redirect(array('controller' => 'gallery', 'action' => 'edit', $this->Photo->getLastInsertID()));
           }
        }
    }
    
    function admin_edit($id)
    {
        $this->layout = 'admin';
        
        $Photos = $this->Photo->find('all');
        $this->set('Photos', $Photos);

        if($this->data)
        {
            if(!isset($this->request->data['Photo']['type'])){$this->request->data['Photo']['type'] = 0;}
            
            if ($this->Photo->save($this->request->data))
            {
                // Set a session flash message and redirect.
                $this->Session->setFlash('Atualizado com sucesso.', 'default', array(), 'good');
                $this->redirect(array('controller' => 'gallery', 'action' => 'edit', $id));
            }
         }
         else
         {
            $this->data = $this->Photo->read(null,$id);
         }
         
//         die(debug($this->data));
        
        
    }
    
    function admin_delete($id)
    {
        $this->Photo->delete($id);
        $this->Session->setFlash('Removido com sucesso.', 'default', array(), 'good');
        $this->redirect('/admin/pages');
    }

    
    // Páginas do SITE AQUI
    function index()
    {
        
    }
}
